#ifndef __CONTROL_H__
#define __CONTROL_H__

#include "gofgame/common/Ref.h"
#include "gofgame/common/WeakRef.h"
#include <vector>
#include <cocos2d.h>
using namespace gofgame::common;
using namespace std;
using namespace cocos2d;

namespace gofgame {
	namespace common {
		namespace forms {
			class Control {
			public:
				class ControlCollection {
				public:
					virtual void add(Control *c);
					virtual void remove(Control *c);
					int count() const;
					Control *operator[] (int index) const;

					virtual ~ControlCollection();

				protected:
					ControlCollection(Control *owner);
					
				private:
					Control *owner;
					Control **first;
					int capacity;
					int len;

				private:
					int lockCount;

				private:
					friend class gofgame::common::forms::Control;
				};

			public:
				template<typename C> static void create(C **output) {
					*output = new C();
				}

				Control();
				virtual ~Control();
				ControlCollection &getControls() const;
				void getParent(Ref<Control>& output) const;

			public:
				void retain();
				void release();

			protected:
				virtual ControlCollection* createControlCollection();
				virtual void onAddedControl(Control *c);
				virtual void onRemovedControl(Control *c);
				virtual void onLayout();

			private:
				ControlCollection *controls;
				WeakRef<Control> parent;
				int refCount;

			private:
				void addedControl(Control *c);
				void removedControl(Control *c);
				friend class gofgame::common::forms::Control::ControlCollection;
				_support_weak_ref(Control);
			};
		}
	}
}

#endif //__CONTROL_H__
